'use strict';

const crypto = require('crypto');
// const fs = require('fs');

class Ctoaes{

    //传base64加密串和加密key ,返回直接数组,将字节数组转化为文件保存即可
    decryptionBinary(inbufBinary, key) {
        var iv = "";
        var clearEncoding = 'binary';
        var cipherEncoding = 'binary';
        var cipherChunks = [];
        
   
        var cipher = crypto.createDecipheriv('aes-128-ecb', key, iv);
        cipherChunks.push(cipher.update(inbufBinary, cipherEncoding,clearEncoding));
        cipherChunks.push(cipher.final(clearEncoding));
        
        var outbuf = cipherChunks.join('');
        return outbuf;
    }

    encryption_binary_cbc(data, key, iv){
        var clearEncoding = 'binary';
        var cipherEncoding = 'binary';
        var cipherChunks = [];
        var cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
        cipher.setAutoPadding(true);
    
        cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));
        cipherChunks.push(cipher.final(cipherEncoding));
    
        return cipherChunks.join('');
    }
    

    getStringHash(str){
        var cipher = crypto.createHash(str);
    }

    encryption_utf82hex(data, key, iv) {
        var clearEncoding = 'utf8';
        var cipherEncoding = 'hex';
        var cipherChunks = [];
        var cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
        cipher.setAutoPadding(true);
    
        cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));
        cipherChunks.push(cipher.final(cipherEncoding));
    
        return cipherChunks.join('');
    }

    decryption_hex2utf8(data, key,iv) {
        var clearEncoding = 'hex';
        var cipherEncoding = 'utf8';
        var cipherChunks = [];
        var cipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
        cipher.setAutoPadding(true);
    
        cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));
        cipherChunks.push(cipher.final(cipherEncoding));
    
        return cipherChunks.join('');
    }
}

module.exports = Ctoaes